/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.factory.server.consumers;

import com.factory.server.json.Plan;
import java.util.LinkedList;

/**
 *
 * @author milos
 */
public class TasksQueue {

    private static LinkedList<Plan> plans;
    private boolean empty = true;
    private long timeQueued;

    public TasksQueue() {
        plans = new LinkedList<Plan>();
        timeQueued = 0;
    }

    public synchronized void put(Plan plan) {
        plans.add(plan);
        empty = false;
        notifyAll();
        System.out.println("Producer: put...");

    }

    public synchronized Plan get() {
        while (empty == true) {	//wait till something appears in the buffer
            try {
                wait();
            } catch (InterruptedException e) {
                //TODO
            }
        }
        if (plans.isEmpty()) {
            empty = true;
        }
        if (plans.size() > 0) {
            return plans.pop();
        }
        else{
            return null;
        }
    }

    public synchronized boolean isEmpty() {
        return empty;
    }

    public long getTimeQueued() {
        return this.timeQueued;
    }

    public synchronized void setPlans(LinkedList<Plan> plans) {
        TasksQueue.plans = plans;
    }

    public LinkedList<Plan> getPlans() {
        return this.plans;
    }

    @Override
    protected TasksQueue clone() {
        TasksQueue copy = new TasksQueue();
        for (Plan plan : plans) {
            copy.put(plan);
        }
        return copy;
    }
}
